﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;

namespace wd_27_shfolder_v1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string desktopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
            //string deskpath2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
            //string deskpath3 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Templates);
            string deskpath4 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu);//开始菜单项
            string deskpath6 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
            string deskpath8 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Recent);//只记录文档，运行的程序记录没有记录
            string deskpath7 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs);//开始最菜单的程序

            string dekpath5 = GetAllUsersDesktopFolderPath();

            //lbldesktop.Text = "当前桌面路径：" + desktopPath + "  |  " + dekpath5;
            string[] xx = Directory.GetDirectories(desktopPath);
            string[] path1 = Directory.GetFiles(desktopPath, "*.lnk");  //用户桌面快捷方式
            string[] path2 = Directory.GetFiles(dekpath5, "*.lnk"); //公共用户桌面快捷方式

            string target_exe = string.Empty;

            List<FilePathModel> lstAllFile = new List<FilePathModel>();
            if (path1 != null && path1.Length > 0)
            {
                foreach (var path in path1)
                {
                    FilePathModel temp = new FilePathModel();

                    temp.Name = Path.GetFileName(path);

                    if ("Vysor.lnk" == temp.Name)
                    {
                        target_exe = path;
                    }

                    temp.Filepath = path;

                    //WshShell shell = new WshShell();
                    //IWshShortcut lnkPath = (IWshShortcut)shell.CreateShortcut(path);

                    //temp.FileTargetPath = lnkPath.TargetPath;
                    lstAllFile.Add(temp);
                }
            }

            Process.Start("explorer.exe", target_exe);  //启动lnk文件

            Thread.Sleep(100);

            dekpath5 = "";

            //if (path1 != null && path2.Length > 0)
            //{
            //    foreach (var path in path2)
            //    {
            //        FilePathModel temp = new FilePathModel();

            //        temp.Name = Path.GetFileName(path);
            //        temp.Filepath = path;
            //        WshShell shell = new WshShell();
            //        IWshShortcut lnkPath = (IWshShortcut)shell.CreateShortcut(path);

            //        temp.FileTargetPath = lnkPath.TargetPath;
            //        lstAllFile.Add(temp);
            //    }
            //}

            //if (txtSearch.Text.Trim() != "")
            //{
            //    lstAllFile = lstAllFile.FindAll(r => r.Name.Contains(txtSearch.Text.Trim()));
            //}
            //c1FlexGrid1.SetDataBinding(lstAllFile, null, true);

        }

        [DllImport("shfolder.dll", CharSet = CharSet.Auto)]
        private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
        private const int MAX_PATH = 260;
        private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
        public static string GetAllUsersDesktopFolderPath()
        {
            StringBuilder sbPath = new StringBuilder(MAX_PATH);
            SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath);
            return sbPath.ToString();
        }
        public class FilePathModel
        {
            public string Name { get; set; }
            public string Filepath { get; set; }
            public string FileTargetPath { get; set; }
        }

        // ————————————————
        // 版权声明：本文为CSDN博主「自律2019」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
        // 原文链接：https://blog.csdn.net/qiqingli/article/details/92798661
    }
}
